<?php

namespace app\site\controller\site;

use app\common\exception\MiException;
use app\model\site\SiteConfig;
use app\site\controller\AController;
use app\site\service\RequestService;
use app\site\service\site\SiteConfigService;
use app\site\service\SiteAuthService;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\Request;

class SiteConfigController extends AController
{

    /**
     * @throws ModelNotFoundException
     * @throws DbException
     * @throws DataNotFoundException
     * @throws MiException
     */
    public function edit(Request $request) {
        $siteConfigService = \app\common\service\CommonSiteConfigService::instance();
        $key = $request->param('key');
        if ($request->isPost()){

            $post = $this->post();
            $post['site_id'] = SiteAuthService::instance()->getSiteId();
            $post['value'] = serialize($post['value']);
            $info = SiteConfig::where('site_id', SiteAuthService::instance()->getSiteId())->where('key', $key)->find();
            if ($info){
                return $this->reqSer->setService(SiteConfigService::instance())->edit($post)->getResponse();
            }else{
                return $this->reqSer->setService(SiteConfigService::instance())->add($post)->getResponse();
            }

        }else{

            $info = $siteConfigService->getConfig(SiteAuthService::instance()->getSiteId(), $key);

            return jsonSuccess([
                'info' => $info,
            ]);
        }


    }

}
